//Aaron Paterson
//LinkedConflict.cpp
//This is the .cpp file that contains the main functionality, append to file, build the linked list, and a conflict finder.
//There is currently one bug that causes issues with the conflict finder but the logic is correct and will all be fully 
// implemented in the final phase.
//Coded by Aaron Paterson

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "LinkedConflict.h"

using namespace std;
//amount of data points we had per line.
int sizeOfNode = 8;

//Default constuctor
LinkedConflict::LinkedConflict(){
	head = NULL;
};
//Overloaded
LinkedConflict::LinkedConflict(string instructor, int start, int end, string department, string section, string room, string building, int course, string day){
	head = new Node(instructor, start, end, department, section, room, building, course, day);
	tail = head;
};
//destructor
LinkedConflict::~LinkedConflict(){};

//Builds the linked list from the file. Should be called after each append, honestly.
void LinkedConflict::buildLinked(string fileName)
{
	//Needed to create io capability with a line and word pull for delimiting purposes.
	//Used a stringstream as sort of a buffer of a line of the text file.
	ifstream myFile;
	myFile.open(fileName);
	string line;
	string word;
	stringstream iss;
	//instantiating variables
	string aInstructor;
	int aStart = 0;
	int aEnd = 0;
	string aDepartment;
	string aSection;
	string aRoom;
	string aBuilding;
	int aCourseNumber = 0;
	string aDays;
	//if a fail of the file opening.
	if (myFile.fail())
	{
		cout << "Error opening file! \n";
	};

	//reads line, reads words delimited by commas and iterates through the line and assigns each
	//placeholder value with that portion of the line depending on the count which is reset outside of the
	//line so it should work fine.
	if (myFile.is_open())
	{
		while (getline(myFile, line))
		{
			iss << line;
			int count = 0;
			while (getline(iss, word, ','))
			{
				if (count == 0)      { string aInstructor = word; }
				else if (count == 1) { int aStart = atoi(word.c_str()); }
				else if (count == 2) { int aEnd = atoi(word.c_str()); }
				else if (count == 3) { string aDepartment = word; }
				else if (count == 4) { string aSection = word; }
				else if (count == 5) { string aRoom = word; }
				else if (count == 6) { string aBuilding = word; }
				else if (count == 7) { int aCourseNumber = atoi(word.c_str()); }
				else if (count == 8) { string aDays = word; }
				count++;
				cout << word << endl;

				LinkedConflict(aInstructor, aStart, aEnd, aDepartment, aSection, aRoom, aBuilding, aCourseNumber, aDays);
			};
			iss.clear();
			cout << endl;
		};
	};
};

//appends to the end of the file containing the class data using outputfilestreams.
void LinkedConflict::appendLinked(string fileName, Node newNode)
{
	ofstream out;

	out.open(fileName, ios::app);

	out << newNode.getInstructor();
	out << ",";
	out << newNode.getStart();
	out << ",";
	out << newNode.getEnd();
	out << ",";
	out << newNode.getDepartment();
	out << ",";
	out << newNode.getSection();
	out << ",";
	out << newNode.getRoom();
	out << ",";
	out << newNode.getBuilding();
	out << ",";
	out << newNode.getDay();
	out << "\n";
};

//small issue with conflict finder at the moment will be ironed out for final phase.
//Checks teachers and days of the class. If they are on same day it checks time. If they are at same time it checks the room.
//It has bool flags for each part so we can do easy comparisons for different cases later.
void LinkedConflict::conflictFinder(Node compareThis)
{
	Node* iter = head;
	bool teacherConflictFound = false;
	bool dayConflictFound = false;
	bool timeConflictFound = false;
	bool roomConflictFound = false;

	int startValue = compareThis.getStart() / 10;
	int endValue = compareThis.getEnd() / 10;

	while (iter != NULL)
	{
		//Check Instructor
		if (compareThis.getInstructor() == iter->getInstructor())
		{
			teacherConflictFound = true;
			cout << "Same Teacher" << endl;
		};

		//Check Day, does not account for MWF being partially the same as MW just yet.
		if (compareThis.getDay() == iter->getDay())
		{
			dayConflictFound = true;
			cout << "Same Days" << endl;
		};

		//Checks time conflicts by dividing military time by 10 the modding by the ends of the iter's start and end time.
		//If the classes overlap (aka the mods are contradicting, i.e. one is 1 and the other 0 or vice versa) an error flag is 
		// changed to true.
		if (dayConflictFound)
		{
			int iterStart = iter->getStart() / 10;
			int iterEnd = iter->getEnd() / 10;

			if (iterStart%startValue >= 1 && iterStart%endValue<1 || iterEnd%startValue >= 1 && iterEnd%endValue<1 || iterStart%startValue<1 && iterEnd%endValue >= 1){
				timeConflictFound = true;
				cout << "Same Time" << endl;
			};

			//if time is conflicting, we check the room. If it's not conflicting we're all good. 
			if (timeConflictFound){
				if (compareThis.getRoom() == iter->getRoom())
				{
					roomConflictFound = true;
					cout << "Same Room" << endl;
				};
			};
		};

		//cases not handled by the comparisons themselves.
		//the couts show what error occoured
		if (teacherConflictFound && timeConflictFound)
		{
			cout << "Same teacher at the same time. BIG PROBLEM!" << endl;
		};
		if (dayConflictFound && timeConflictFound && roomConflictFound)
		{
			cout << "Same room used by two classes. BIG PROBLEM!" << endl;
		};

		//iterates to the next, although I believe there was a bug in how it was built.
		iter = iter->next;
	};
};
